#region License

// Copyright (c) 2013, ClearCanvas Inc.
// All rights reserved.
// http://www.clearcanvas.ca
//
// This file is part of the ClearCanvas RIS/PACS open source project.
//
// The ClearCanvas RIS/PACS open source project is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// The ClearCanvas RIS/PACS open source project is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// the ClearCanvas RIS/PACS open source project.  If not, see
// <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System.IO;

namespace ClearCanvas.Utilities.BuildTasks
{
    public class VersionProjects : Microsoft.Build.Utilities.Task
    {
        public override bool Execute()
        {
            string[] list = this._assemblyInfoFileList.Split(';');
            foreach (string file in list)
            {
                //Read File
                StreamReader reader = new StreamReader(file);
                string fileline;
                string attribute;
                List<string> usingStatements = new List<string>();
                List<string> assemblyInfoAttributes = new List<string>(_assemblyInfoSuppliedAttributes);
                while ((fileline = reader.ReadLine()) != null)
                {
                    //Grab using Statement Lines
                    if (fileline.ToLower().StartsWith("using"))
                    {
                        usingStatements.Add(fileline);
                    }
                    //Parse Lines with Attributes
                    if (fileline.ToLower().StartsWith("[assembly: "))
                    {
                        if (fileline.Contains("("))
                        {
                            attribute = fileline.Substring(11, fileline.IndexOf('(') - 11);
                        }
                        else
                        {
                            attribute = fileline.Substring(11, fileline.LastIndexOf(']') - 11);
                        }
                        if (!_attributesEncountered.Contains(attribute))
                        {
                            assemblyInfoAttributes.Add(fileline);
                        }
                    }
                    else if (fileline.ToLower().StartsWith("[assembly : "))
                    {
                        if (fileline.Contains("("))
                        {
                            attribute = fileline.Substring(12, fileline.IndexOf('(') - 12);
                        }
                        else
                        {
                            attribute = fileline.Substring(12, fileline.LastIndexOf(']') - 12);
                        }
                        if (!_attributesEncountered.Contains(attribute))
                        {
                            assemblyInfoAttributes.Add(fileline);
                        }
                    }
                }
                reader.Close();
                //Write file
                StreamWriter writer = new StreamWriter(file, false);
                //insert some header to indicate that the file is autogenerated
                usingStatements.ForEach(delegate(string line) { writer.WriteLine(line); });
                assemblyInfoAttributes.ForEach(delegate(string line) { writer.WriteLine(line); });
                writer.Flush();
                writer.Close();
                assemblyInfoAttributes.Clear();
            }
            return true;
        }

        private delegate void writeAssemblyInfoLineHandler(string line, StreamWriter writer);
        private void writeAssemblyInfoLine(string line, StreamWriter writer)
        {
            writer.WriteLine(line);
        }

        [Required]
        public string AssemblyInfoFileList
        {
            set { _assemblyInfoFileList = value; }
        }

        #region Assembly Attributes
        public string AssemblyTitle
        {
            set
            {
                _attributesEncountered.Add("AssemblyTitle");
                _assemblyInfoSuppliedAttributes.Add("[assembly: AssemblyTitle(\"" + value + "\")]");
            }
        }

        public string AssemblyDescription
        {
            set
            {
                _attributesEncountered.Add("AssemblyDescription");
                _assemblyInfoSuppliedAttributes.Add("[assembly: AssemblyDescription(\"" + value + "\")]");
            }
        }

        public string AssemblyConfiguration
        {
            set
            {
                _attributesEncountered.Add("AssemblyConfiguration");
                _assemblyInfoSuppliedAttributes.Add("[assembly: AssemblyConfiguration(\"" + value + "\")]");
            }
        }

        public string AssemblyCompany
        {
            set
            {
                _attributesEncountered.Add("AssemblyCompany");
                _assemblyInfoSuppliedAttributes.Add("[assembly: AssemblyCompany(\"" + value + "\")]");
            }
        }

        public string AssemblyProduct
        {
            set
            {
                _attributesEncountered.Add("AssemblyProduct");
                _assemblyInfoSuppliedAttributes.Add("[assembly: AssemblyProduct(\"" + value + "\")]");
            }
        }

        public string AssemblyCopyright
        {
            set
            {
                _attributesEncountered.Add("AssemblyCopyright");
                _assemblyInfoSuppliedAttributes.Add("[assembly: AssemblyCopyright(\"" + value + "\")]");
            }
        }

        public string AssemblyTrademark
        {
            set
            {
                _attributesEncountered.Add("AssemblyTrademark");
                _assemblyInfoSuppliedAttributes.Add("[assembly: AssemblyTrademark(\"" + value + "\")]");
            }
        }

        public string AssemblyCulture
        {
            set
            {
                _attributesEncountered.Add("AssemblyCulture");
                _assemblyInfoSuppliedAttributes.Add("[assembly: AssemblyCulture(\"" + value + "\")]");
            }
        }

        public string ComVisible
        {
            //Boolean Value hence no quotes
            set
            {
                _attributesEncountered.Add("ComVisible");
                _assemblyInfoSuppliedAttributes.Add("[assembly: ComVisible(" + value + ")]");
            }
        }

        public string Guid
        {
            set
            {
                _attributesEncountered.Add("Guid");
                _assemblyInfoSuppliedAttributes.Add("[assembly: Guid(\"" + value + "\")]");
            }
        }

        public string AssemblyVersion
        {
            set
            {
                _attributesEncountered.Add("AssemblyVersion");
                _assemblyInfoSuppliedAttributes.Add("[assembly: AssemblyVersion(\"" + value + "\")]");
            }
        }

        public string AssemblyFileVersion
        {
            set
            {
                _attributesEncountered.Add("AssemblyFileVersion");
                _assemblyInfoSuppliedAttributes.Add("[assembly: AssemblyFileVersion(\"" + value + "\")]");
            }
        }
        #endregion Assembly Attributes

        private string _assemblyInfoFileList;
        private List<string> _assemblyInfoSuppliedAttributes = new List<string>();
        private List<string> _attributesEncountered = new List<string>();
    }
}